<template>
    <page-container title="资讯分类">
        <template #header></template>
        <template #extra>
            <el-button @click="onAddCate">添加分类</el-button>
        </template>
        <!-- 主体部分 -->
         <el-table v-loading="loading" :data="cateList" style="width: 100%;">
            <el-table-column type="index" label="序号" width="100"></el-table-column>
            <el-table-column prop="name" label="名字"></el-table-column>
            <el-table-column label="主图"></el-table-column>
            <el-table-column prop="desc" label="描述"></el-table-column>
            <el-table-column label="操作" align="center">
                <!-- row是cateList中的每一项,$index是当前的索引 -->
                <template #default="{row,$index}">
                    <el-button @click="onEdit(row,$index)">编辑</el-button>
                    <el-button
                        type="danger"
                        @click="onDelete(row,$index)"
                        >
                        删除
                    </el-button>
                </template>
            </el-table-column>
            <template>
                <el-empty description="暂无数据" />
            </template>
         </el-table>

         <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
        
    </page-container>
</template>
  
<script setup>
import { artCateDeleteRequest, artCateListRequest } from '@/api/article';
import { ref } from 'vue';
import ChannelEdit from './components/ChannelEdit.vue';
import { ElMessage, ElMessageBox } from 'element-plus';

  
const cateList = ref([]);
const loading = ref(false);
const dialog = ref(null);
// const dialogVisible = ref(false);

  function loadList(){
    loading.value = true
    artCateListRequest().then(r=>{
      console.log(r.data.data)
      cateList.value = r.data.data
      loading.value = false
    })
    
  }
  loadList()

  function onEdit(row,index){
    console.log(row,index)
    dialog.value.openDialog(row)
  }

  function onDelete(row,index){
    console.log(row,index)
    ElMessageBox.confirm('确定要删除吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
    
    artCateDeleteRequest(row.id).then(r=>{
      console.log(r.data);
      ElMessage.success('删除成功')
      loadList()
    })
  });
  }

  function onAddCate(){
    dialog.value.openDialog({})
  }

  function onSuccess(){
    loadList()
  }
</script>
  
<style>
  
</style>